// pages/bmfw/details/details.js
const con = require("../../../../utils/data.js");
const WXAPI = require('../../../../wxapi/main')
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: [],
    shopId:0,
      cLatitude:'',
      cLongitude:'',
      cHaircut: '', //剪发总次数
      cSurplus: '', //剪发剩余次数
      mHaircut: '', //按摩椅总次数
      mSurplus: '', //按摩椅剩余次数
      mSwitch: '',
      cSwitch: '',
      hair_open: '',
      chair_open: '',
      cId: '',
      outTradeNo: '',
      prepay_id: ''
  },
  goHome(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  gohair(e) {
      console.log("e---"+JSON.stringify(e));
      var shopId = e.currentTarget.dataset.id;
      console.log("shopId---"+shopId);
        wx.navigateTo({
            url: '../../hair/hair?shopId='+shopId,
        })
    },
  location(){
    let cLatitude = this.data.details.barberShop.latitude;
    let cLongitude = this.data.details.barberShop.longitude;
    wx.navigateTo({
      url: '../../details/location/location?cLatitude='+cLatitude+'&cLongitude='+cLongitude,
    })
  },
  phone(){
    wx.makePhoneCall({
      phoneNumber: this.data.details.barberShop.telephone
    })
  },
    // 弹出核销模态框
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })

    },
    familyShare(){
        // 暂时注释
        wx.showToast({
            title: '服务暂未开启',
            icon: 'loading'
        })
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    //立即消费/共享消费一次
    Verification() {
        var that = this;
        var shopId= that.data.shopId;
        let familySurplus = this.data.details.familySurplus;
        const options = {};
        var url;
        if (familySurplus!=''){
            // url='http://3s.dkys.org:14653/api/community/shareHaircut';
            url='https://www.brvpmusic.com/api/community/shareHaircut';
        } else {
            url= 'https://www.brvpmusic.com/api/community/barberConsumption';
        }
        wx.request({
            url: url,
            // url: con.index_haircutx,
            data: {
                'openid': wx.getStorageSync('OpenId'),
                'shopId': that.data.shopId
            },
            method: 'GET',
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                console.log('返回成功');
                options.shopId = shopId;
                that.getDetail(options);
                that.hideModal();
                if (res.data.code == 200) {
                    wx.showToast({
                        title: '消费成功',
                    })
                }
                if (res.data.code == 300) {
                    wx.showToast({
                        title: '次数不足',
                    })
                }
            },
            fail: function(res) {
                console.log('消费失败');
            }
        })
    },
    //接触绑定状态
    relieveShare() {
        var that = this;
        var shopId= that.data.shopId;
    },
    //家庭共享
    family(e) {
        console.log(e);
        var tel = e.detail.value.tel;
        let that = this;
        wx.request({
            // url: 'https://www.brvpmusic.com/customers_hair_char/hairCutByFamily',
            // url:'http://3s.dkys.org:14653/api/community/hairCutByFamily',
            url:'https://www.brvpmusic.com/api/community/hairCutByFamily',
            data: {
                'openid': wx.getStorageSync('OpenId'),
                'tel': tel,
                'shopId':that.data.shopId
            },
            method: 'GET',
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                console.log('返回成功');
                that.hideModal();
                    wx.showToast({
                        title: res.data.message,
                        icon:"none"
                    })
            },
            fail: function(res) {
                console.log('返回失败');
            }
        })
    },

    getDetail(options){
        let that=this;
        wx.showLoading({
            mask: true,
            title:'数据加载中...',
        });
        wx.request({
            // url: con.barbershop_details,
            //   url:'http://3s.dkys.org:14653/api/community/getHaircutCard',
            url:'https://www.brvpmusic.com/api/community/getHaircutCard',
            method: 'GET',
            data: {
                'shopId': this.data.shopId,
                'openid':wx.getStorageSync("OpenId")
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function (res) {
                console.log(res.data);
                console.log('返回成功');
                that.setData({
                    details: res.data.body,
                    cLatitude: res.data.body.barberShop.latitude,
                    cLongitude: res.data.body.barberShop.longitude,

                })
                wx.hideLoading();
            },
            fail: function (res) {
                console.log('返回失败');
            }
        })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    that.setData({
      shopId: options.shopId,
    })
      this.getDetail(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})